var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var time = 0;
var touchDot = 0;//触摸时的原点
var interval = "";
var _Cart= require("../../modules/cart_module.js");
Page({
  data: {
    navList: [],
    name:'',
    goodsList: [],
    id: 0,
    currentIndex:0,
    currentCategory: {},
    scrollLeft: 0,
    leftNavId:'',
    rightNavId:'',
    scrollTop: 0,
    scrollHeight: 0,
    page: 1,
    cartNum:0,
    size: 10000
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    if (options.search) {
      that.setData({
        name: options.search
      });
    }

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });

      this.getCategoryInfo();
      this.getCartNum()

  },
  gotoIndex: function(){
      wx.switchTab({
      url: '/pages/index/index',
      })
  },
  getCartNum:function(){
    let _this = this;
    util.request(api.cartUrl.cartList).then((res) =>{
      _this.setData({
        cartNum:res.result.count
      })
    });
  },
  gotoCart:function(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
    onPullDownRefresh: function () {
        this.getCategoryInfo();
    },
  touchNavTo: function (id) {
      if (this.data.id == id) {
          return false;
      }
      this.setData({
          id: id
      });

      // this.getCategoryInfo();
  },
  // 触摸开始事件
  touchStart: function (e) {
      touchDot = e.touches[0].pageX; // 获取触摸时的原点
      // 使用js计时器记录时间
      interval = setInterval(function () {
          time++;
      }, 100);
  },
  // 触摸结束事件
  touchEnd: function (e) {
      var touchMove = e.changedTouches[0].pageX;
      // 向左滑动
      if (touchMove - touchDot <= -60 && time < 10) {
          //执行切换页面的方法
          if(this.data.rightNavId){
              this.touchNavTo(this.data.rightNavId)
          }

      }
      // 向右滑动
      if (touchMove - touchDot >= 60 && time < 10) {
          //执行切换页面的方法
          if(this.data.leftNavId){

              this.touchNavTo(this.data.leftNavId)

          }

      }
      clearInterval(interval); // 清除setInterval
      time = 0;
  },
  getCategoryInfo: function () {
    let that = this;
    wx.showLoading({
      title: '正在加载',
    })
    util.request(api.FindCommodityByChangeAttribute, { name: this.data.name },'POST')
      .then(function (res) {

        if (res.code == 200) {
          that.setData({
            goodsList: res.result
          });

        }
        
      }).finally(()=>{
        wx.hideLoading()
      })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  getGoodsList: function () {
    var that = this;

    util.request(api.GoodsList, {categoryId: that.data.id, page: that.data.page, size: that.data.size})
      .then(function (res) {
        that.setData({
          goodsList: res.data.goodsList,
        });
      });
  },
  onUnload: function () {
    // 页面关闭
  },
  addCart(event){
    this.cartUpdate(event.currentTarget.dataset.id,1)
  },
  minusCart(event){
    this.cartUpdate(event.currentTarget.dataset.id,-1)
  
  },
  async cartUpdate(id,update){
    const list = this.data.goodsList;
    // 请求
    const res = await _Cart._cartUpdate({
      id:id,
      update: update,
      list,
      changeProperty: "hasNumber"
    }).catch(err=>{
      console.log(err)
    });
    // 本地更新购物车数量
    if(res.id){
      const item_str = 'goodsList[' + res.index + '].hasNumber'
      this.getCartNum();
      this.setData({
        [item_str]: res.num
      })
    }  
  },
  switchCate: function (event) {
    if (this.data.id == event.currentTarget.dataset.id) {
      return false;
    }
    var that = this;
    var clientX = event.detail.x;
    var currentTarget = event.currentTarget;
    if (clientX < 60) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft - 60
      });
    } else if (clientX > 330) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft
      });
    }
    this.setData({
      id: event.currentTarget.dataset.id
    });

    // this.getCategoryInfo();
  }
})